package me.monster.smarttraffic.ui.login;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import butterknife.BindView;
import butterknife.OnClick;
import me.monster.smarttraffic.R;
import me.monster.smarttraffic.base.AbstractPresenterActivity;
import me.monster.smarttraffic.ui.dash.DashActivity;

/**
 * @author jiangjiwei
 */
public class LoginActivity extends AbstractPresenterActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.et_login_account)
    EditText etAccount;
    @BindView(R.id.et_login_password)
    EditText etPassword;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.pb_login)
    ProgressBar pbLogin;

    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View closeToolView() {
        return findViewById(R.id.iv_login_logo);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public String getInitTitle() {
        return getString(R.string.title_activity_login);
    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @OnClick(R.id.bt_login)
    public void onLoginClick() {
        mPresenter.login(etAccount.getText().toString().trim(), etPassword.getText().toString().trim());
    }

    @Override
    public void showError(int errorCode) {
        switch (errorCode) {
            case LoginPresenter.ACCOUNT_EMPTY:
                etAccount.requestFocus();
                toast("账户不能为空");
                break;
            case LoginPresenter.ACCOUNT_SHORT:
                toast("账户名格式错误");
                etAccount.requestFocus();
                break;
            case LoginPresenter.PASSWORD_EMPTY:
                toast("密码不能为空");
                etPassword.requestFocus();
                break;
            case LoginPresenter.PASSWORD_SHORT:
                toast("密码长度太小");
                etPassword.requestFocus();
                break;
            default:
        }
    }

    @Override
    public void loginResult(boolean success) {
        dismissLoading();
        if (success) {
            toast("登录成功");
            DashActivity.start(this);
            this.finish();
        }
    }

    @Override
    public void showLoading() {
        pbLogin.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissLoading() {
        pbLogin.setVisibility(View.GONE);
    }
}
